import QtQuick 1.0
import "../Components/"

DialogBase {
    id: _root
    width: calcWidth()//(parent.width < _item.width + 10 + 10) ? parent.width : _item.width + 10 + 10
    height: calcHeight()//titleHeight + _item.height + 10 + 10
    x: 0
    y: 0

    signal clickOkButton        //OKボタンのイベント
    signal clickCancelButton    //cancelボタンのイベント

    //ダイアログの本文
    property alias dialogMessage: _text.text
    property alias visibleCancel: _buttonCancel.visible


    // 表示された時の処理
    function viewEvent(){
        _buttonOk.forceActiveFocus();
    }

    // 幅の計算
    function calcWidth(){
        var btn_width = _areaButton.width * (visibleCancel ? 1 : 2);
        var txt_width = _text.paintedWidth;
        var parent_width = _root.parent.width;
        var temp_width = 0;

        if(txt_width < btn_width){
            temp_width = btn_width + 20;
        }else{
            temp_width = txt_width + 20;
        }
        if(temp_width < parent_width){
            // そのまま採用
        }else{
            temp_width = parent_width - border.width * 2;
        }
        return temp_width;
    }
    // 高さの計算
    function calcHeight(){
        var temp_height = titleHeight + _item.height + 10 + 10;
        var parent_height = _root.parent.height;
        if(temp_height < parent_height){
            // そのまま
        }else{
            temp_height = parent_height - border.width * 2;
        }
        return temp_height;
    }

    Column {
        id: _item
        anchors.top: _root.titleBottom
        anchors.right: _root.right
        anchors.left: _root.left
        anchors.margins: 10
        spacing: 10

        Text {
            id: _text
            x: 0
            y: 0
            text: "message"
            wrapMode: Text.WordWrap

            font.pixelSize: _Setting.fontPixelSizeDlgMessageS * density
            color: "#ffffff"
        }

        Row {
            id: _areaButton
            anchors.horizontalCenter: _item.horizontalCenter

            spacing: 5

            Button {
                id: _buttonOk
                width: _buttonCancel.width
                text: "O K"
                font.pixelSize: _Setting.fontPixelSizeButton * _root.density

                onClicked : {
                    clickOkButton();
                }

                Keys.onTabPressed: {
                    if(_buttonCancel.visible){
                        _buttonCancel.forceActiveFocus();
                    }
                }
                Keys.onPressed: {
                    switch(event.key){
                    case Qt.Key_Escape:
                        clickCancelButton();
                        break;
                    case Qt.Key_Enter:
                    case Qt.Key_Return:
                        clickOkButton();
                        break;
                    default:
                        break;
                    }
                }
            }

            Button {
                id: _buttonCancel
                text: "Cancel"
                font.pixelSize: _Setting.fontPixelSizeButton * _root.density

                onClicked : {
                    clickCancelButton();
                }

                Keys.onTabPressed: {
                    _buttonOk.forceActiveFocus();
                }
                Keys.onPressed: {
                    switch(event.key){
                    case Qt.Key_Escape:
                    case Qt.Key_Enter:
                    case Qt.Key_Return:
                        clickCancelButton();
                        break;
                    default:
                        break;
                    }
                }
            }
        }

    }
}
